package com.hqhy.like.stock.dao.persist.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.util.PageInfoToPageDataConverter;
import com.hqhy.like.stock.dao.persist.mapper.StockOutMapper;
import com.hqhy.like.stock.dao.persist.repository.IStockOutRepository;
import com.hqhy.like.stock.pojo.entity.StockOut;
import com.hqhy.like.stock.pojo.param.StockSearchParam;
import com.hqhy.like.stock.pojo.vo.StockOutListItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Repository
public class StockOutRepositoryImpl implements IStockOutRepository {
    @Autowired
    private StockOutMapper stockOutMapper;

    @Override
    public int insert(StockOut stockOut) {
        return stockOutMapper.insert(stockOut);
    }

    @Override
    public int deleteById(Long id) {
        return stockOutMapper.deleteById(id);
    }

    @Override
    public int updateById(StockOut stockOut) {
        return stockOutMapper.updateById(stockOut);
    }

    @Override
    public PageData<StockOutListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询出库列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<StockOutListItemVO> list = stockOutMapper.list();
        PageInfo<StockOutListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public StockOutListItemVO selectStockOutById(Long id) {
        return stockOutMapper.selectStockInById(id);
    }

    @Override
    public PageData<StockOutListItemVO> listForSearch(StockSearchParam searchParam) {
        log.debug("开始执行【查询入库列表】的数据访问，格式化前的参数：{}",searchParam);
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (searchParam.getStart()!=null&&searchParam.getEnd()!=null){
            searchParam.setStartTime(LocalDate.parse(searchParam.getStart(),pattern).atStartOfDay());
            searchParam.setEndTime(LocalDate.parse(searchParam.getEnd(),pattern).plusDays(1).atStartOfDay());
        }
        log.debug("开始执行【查询入库列表】的数据访问，格式化后的参数：{}",searchParam);
        PageHelper.startPage(searchParam.getPageNum(), searchParam.getPageSize());
        List<StockOutListItemVO> list = stockOutMapper.listForSearch(searchParam);
        PageInfo<StockOutListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<StockOutListItemVO> listStockInByIds(List<Long> ids) {
        return stockOutMapper.listStockInByIds(ids);
    }

    @Override
    public StockOutListItemVO getStockOutById(Long id) {
        return stockOutMapper.selectStockInById(id);
    }

}
